package com.chinawayltd.ews.sample.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.manager.redis.MainAndMonitorDateManager;
import com.chinawayltd.ews.sample.mapper.TaskProducerMapper;
import com.chinawayltd.ews.sample.model.openApi.TaskScreenDTO;
import com.chinawayltd.ews.sample.service.SynchroMonitorDataService;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-05-25 13:17
 **/
@Service
public class SynchroMonitorDataServiceImpl implements SynchroMonitorDataService {

    @Autowired
    private TaskProducerMapper taskProducerMapper;
    @Autowired
    MainAndMonitorDateManager mainAndMonitorDateManager;
    
	@Override
	public void synchroMonitorData() throws Exception {
		List<TaskScreenDTO> listAllTaskOnScreen = taskProducerMapper.listAllTaskOnScreen();
    	mainAndMonitorDateManager.deleteAllScreenTask();
    	mainAndMonitorDateManager.deleteListOrderDetail();
    	for (int i = 0; i < listAllTaskOnScreen.size(); i++) {
    		TaskScreenDTO dto = listAllTaskOnScreen.get(i);
    		String jsonString = JSONObject.toJSONString(dto);
        	mainAndMonitorDateManager.setAllScreenTask(jsonString);
    		Long taskId = dto.getId();
    		if(taskId !=null){
    			List<Map<String, Object>> listOrderDetailById = taskProducerMapper.listOrderDetailByTaskId(taskId);
        		if(listOrderDetailById!=null && !listOrderDetailById.isEmpty()){
        			String jsonStr = JSONObject.toJSONString(listOrderDetailById);
            		mainAndMonitorDateManager.setListOrderDetailByTaskId(dto.getId().toString(), jsonStr);
        		}
    		}
		}
	}
	
}
